﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Xml;
using System.Text.RegularExpressions;

namespace VipTestConfig
{
    public partial class FrmMain : Form
    {
        private static string PATH = "HQ\\";
        private static string SHHQ = "show2003.dbf";
        private static string SZHQ = "sjshq.dbf";
        private int iAmount;
        private string strMarket;


        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnSH_Click(object sender, EventArgs e)
        {
            if (!File.Exists(PATH + SHHQ))
            {
                MessageBox.Show("行情文件不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.strMarket = "1";
            string strAmount = checkAmount();
            if (strAmount == null)
            {
                MessageBox.Show("买入股数输入有误，必须为整数，且为100的倍数!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else 
            {
                this.iAmount = Convert.ToInt32(strAmount);
            }

            OleDbDataReader reader = getHQ("SH");
            generateXML(reader);
            MessageBox.Show("批量委托配置文件生成成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnSZ_Click(object sender, EventArgs e)
        {
            if (!File.Exists(PATH + SZHQ))
            {
                MessageBox.Show("行情文件不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.strMarket = "2";
            string strAmount = checkAmount();
            if (strAmount == null)
            {
                MessageBox.Show("买入股数输入有误，必须为整数，且为100的倍数!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                this.iAmount = Convert.ToInt32(strAmount);
            }

            OleDbDataReader reader = getHQ("SZ");
            generateXML(reader);
            MessageBox.Show("批量委托配置文件生成成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private OleDbDataReader getHQ(string market)
        {
            string strDataSource = "";
            string strSQL = "";
            OleDbDataReader reader = null;

            if (market.ToUpper() == "SH")
            {
                strDataSource = PATH + SHHQ;
                strSQL = "select top 150 s1 as SEC_CODE, s2 as SEC_NAME, " + this.iAmount.ToString() + "  as AMOUNT, ROUND(s3*1.1, 2) as PRICE from " + SHHQ + " where s1 like '6%' and s4 <> 0 order by s1";
            }
            else 
            {
                strDataSource = PATH + SZHQ;
                strSQL = "select top 150 HQZQDM as SEC_CODE, HQZQJC as SEC_NAME, " + this.iAmount.ToString() + " as AMOUNT, ROUND(HQZRSP*1.1, 2) as PRICE from " + SZHQ + " where HQZQDM like '0%' and HQJRKP <> 0 and HQZQDM <> '000000' order by HQZQDM";
            }

            try
            {
                OleDbConnection conn = new OleDbConnection();
                conn.ConnectionString = "Provider=VFPOLEDB.1;Data Source=" + strDataSource + ";Persist Security Info=True";
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSQL;
                conn.Open();
                reader = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return reader;
        }

        private void generateXML(OleDbDataReader reader)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = xmlDoc.CreateElement("Entrusts");
            root.SetAttribute("MARKET", this.strMarket);

            xmlDoc.AppendChild(xmlDec);
            xmlDoc.AppendChild(root);

            while (reader.Read())
            {
                XmlElement xmlNode = xmlDoc.CreateElement("Entrust");
                xmlNode.SetAttribute("SEC_CODE", reader["SEC_CODE"].ToString());
                xmlNode.SetAttribute("SEC_NAME", reader["SEC_NAME"].ToString());
                xmlNode.SetAttribute("AMOUNT", reader["AMOUNT"].ToString());
                xmlNode.SetAttribute("PRICE", reader["PRICE"].ToString());
                root.AppendChild(xmlNode);
            }

            xmlDoc.Save("entrusts.xml");
        }

        private string checkAmount()
        {
            int iTemp = 100;
            if (this.txtAmount.Text == "")
            {
                return iTemp.ToString();
            }
            else 
            {
                if (IsNumeric(this.txtAmount.Text))
                {
                    if (Convert.ToInt32(this.txtAmount.Text) % 100 == 0)
                    {
                        return this.txtAmount.Text;
                    }
                }

                return null;
            }
        }

        private bool IsNumeric(string strNumber)
        {
            Regex regex = new Regex("[1-9]+[0-9]*");
            return regex.IsMatch(strNumber);
        }
        
    }
}
